package com.itsci.assignment2;

import java.util.ArrayList;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ListGuide extends Activity {
//	private EditText ed;
	private ListView lv;
	private GuideManager guideMgr;
	private ArrayList<String>  array = new  ArrayList<String>();
	private ArrayList<GuideBean> array_guide = new ArrayList<GuideBean>();
	private int point;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_guide);
		guideMgr = new GuideManager(ListGuide.this,null,null,1);
		array_guide = guideMgr.getAllRegisterBean();
		for(int i=0;i<array_guide.size();i++){
			array.add(array_guide.get(i).getGuidName());
		}
		lv  = (ListView)findViewById(R.id.listView_listGuide);
		lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
		registerForContextMenu(lv);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				point = position;
			}
		});
		
		
		
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Add Guide");
		return super.onCreateOptionsMenu(menu);

	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Intent gotoAdd = new Intent(ListGuide.this,AddGuide.class);
			startActivity(gotoAdd);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);	
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (v.getId() == R.id.listView_listGuide) {
			menu.setHeaderTitle("Menu");
			menu.add(0,1,0,"Detail");
			menu.add(0,2,0,"Delete");
		}
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Intent gotoDetail = new Intent(ListGuide.this,ShowGuide.class);
			gotoDetail.putExtra("name", lv.getItemAtPosition(point).toString());
			startActivity(gotoDetail);
			break;
		case 2:
			@SuppressWarnings("unused")
			int n = guideMgr.getId( lv.getItemAtPosition(point).toString());
			guideMgr.deleteGuide(n);
			Intent g = new Intent(ListGuide.this,ListGuide.class);
			g.putExtra("name", lv.getItemAtPosition(point).toString());
			startActivity(g);
			break;
		
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
	
}
